package com.swanclouds.redfish.managers;

import com.google.common.base.Preconditions;
import com.swanclouds.api.managers.VirtualMediaService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.model.managers.VirtualMedia;
import com.swanclouds.model.managers.VmmControl;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseManagerService;
import com.swanclouds.redfish.managers.domain.VirtualMediaModel;
import java.util.ArrayList;
import java.util.List;


public class VirtualMediaServiceImpl extends BaseManagerService implements VirtualMediaService {

    /**
     *查询虚拟设备集合资源信息
     * @return CollectionResource
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.PATH_MANAGERS_VIRTUALMEDIA,getManagerId())).execute();
    }

    /**
     * 查询虚拟设备集合资源详情信息
     * @return java.util.List
     */
    @Override
    public List<VirtualMedia> list() {
        List<VirtualMedia> virtualMediaList = new ArrayList<>();
        CollectionResource resource = get();
        resource.getCollectionResourceList().stream().forEach(ref -> {
            virtualMediaList.add(get(VirtualMediaModel.class, uri(ref)).execute());
        });
        return virtualMediaList;
    }

    /**
     * 根据ID查询虚拟媒体信息
     * @param virtualMediaId 虚拟媒体ID 必填
     * @return VirtualMedia
     */
    @Override
    public VirtualMedia getVirtualMedia(String virtualMediaId) {
        Preconditions.checkNotNull(virtualMediaId, "virtualMediaId is null");
        return get(VirtualMediaModel.class,uri(ClientConstants.PATH_MANAGERS_VIRTUALMEDIA_ID,getManagerId(),virtualMediaId)).execute();
    }

    /**
     * 服务器弹出卸载虚拟媒体
     * @param vmmControl [VirtualMediaId 路径后虚拟磁盘ID,image 镜像ip+绝对路径+镜像名称 VmmControlType 虚拟媒体操作类型]
     * @return ActionResponse
     */
    @Override
    public ActionResponse virtualMediaEjectMedia(VmmControl vmmControl) {
        Preconditions.checkNotNull(vmmControl.getVirtualMediaId(), "VirtualMediaId is null");
        VirtualMedia virtualMedia = getVirtualMedia(vmmControl.getVirtualMediaId());
        if (virtualMedia.getOem() == null){
            return post(ActionResponse.class,uri(ClientConstants.PATH_MANAGERS_VIRTUALMEDIA_EJECTMEDIA,getManagerId(),vmmControl.getVirtualMediaId())).execute();
        }else {
            Preconditions.checkNotNull(vmmControl.getVmmControlType(), "VmmControlType is null");
            return post(ActionResponse.class,uri(ClientConstants.PATH_MANAGERS_VIRTUALMEDIA_VMMCONTROL,getManagerId(),vmmControl.getVirtualMediaId()))
                    .entity(vmmControl)
                    .execute();
        }
    }

    /**
     * 服务器挂载虚拟设备
     * @param vmmControl [VirtualMediaId 路径后虚拟磁盘ID,image 镜像ip+绝对路径+镜像名称 VmmControlType 虚拟媒体操作类型]
     * @param virtualMediaId Path 必填 虚拟磁盘ID
     * @return the virtual media
     */
    @Override
    public VirtualMediaModel virtualMediaInsertMedia(VmmControl vmmControl,String virtualMediaId) {
        Preconditions.checkNotNull(virtualMediaId, "VirtualMediaId is null");
        Preconditions.checkNotNull(vmmControl.getImage(), "image is null");
        return post(VirtualMediaModel.class,uri(ClientConstants.PATH_MANAGERS_VIRTUALMEDIA_INSERTMEDIA,getManagerId(),virtualMediaId)).entity(vmmControl).execute();
    }
}
